﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team (cellcloudproject@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellFileExpressTask_h
#define CellFileExpressTask_h

#include "CellPrerequisites.h"
#include "CellRunnable.h"
#include "CellMessageHandler.h"

namespace cell
{

/// 未知状态
#define EXPRESS_STATE_UNKNOWN 0
/// 通信连接丢失状态
#define EXPRESS_STATE_LOST 1
/// 属性确认状态
#define EXPRESS_STATE_ATTR 2
/// 准备状态
#define EXPRESS_STATE_PREPARE 3
/// 开始状态
#define EXPRESS_STATE_BEGIN 4
/// 数据传输状态
#define EXPRESS_STATE_DATA 5
/// 结束状态
#define EXPRESS_STATE_END 6
/// 未通过授权检查
#define EXPRESS_STATE_UNAUTH 9
/// 任务完成状态
#define EXPRESS_STATE_EXIT 11


/** 文件传输任务类。

Upload 流程：
C-S Auth
S-C Auth
C-S Attr
S-C Attr
C-S Begin
S-C Begin
C-S Data *
S-C Data-Receipt *
C-S End
S-C End

Download 流程：
C-S Auth
S-C Auth
C-S Attr
S-C Attr
C-S Begin
S-C Begin
C-S Offer
S-C Data *
C-S Data-Receipt *
S-C End
C-S End
*/
class _CellPrivate FileExpressTask : public Runnable, public MessageHandler
	, public ThreadAlloc
{
public:
	/**
	*/
	FileExpressTask(FileExpressContext* context);

	/**
	*/
	virtual ~FileExpressTask(void);

public:
	/** 线程回调入口。
	*/
	void run(void);

	/** 放弃执行。
	*/
	void abort(void);

	/** 返回上下文。
	*/
	FileExpressContext* getContext(void) const;

	/** 设置监听器。
	*/
	void setListener(FileExpressListener* listener);

protected:
	/** 创建连接会话。
	*/
	void sessionCreated(Session* session);

	/** 销毁连接会话。
	*/
	void sessionDestroyed(Session* session);

	/** 开启连接会话。
	*/
	void sessionOpened(Session* session);

	/** 关闭连接会话。
	*/
	void sessionClosed(Session* session);

	/** 接收到消息。
	*/
	void messageReceived(Session* session, Message* message);

	/** 消息已发送。
	*/
	void messageSent(Session* session, Message* message);

	/** 发生错误。
	*/
	void errorOccurred(uint16 errorCode, Session* session);

private:
	/**
	*/
	void interpret(Session* session, Packet* packet);

	/**
	*/
	void upload(void);

	/**
	*/
	void download(void);

private:
	/**
	*/
	void postAuthCode(void);

	/**
	*/
	void queryFile(void);

	/**
	*/
	void beginUpload(ResultSet* resultSet);

	/**
	*/
	void endUpload(ResultSet* resultSet);

	/**
	*/
	bool processUploadData(ResultSet* resultSet);

	/**
	*/
	void beginDownload(void);

	/**
	*/
	void endDownload(ResultSet* resultSet);

	/**
	*/
	void offerDownload(ResultSet* resultSet);

	/**
	*/
	void processDownloadData(ResultSet* resultSet);

private:
	FileExpressContext* mContext;
	BlockingConnector* mConnector;

	long mProgress;
	char* mDataCache;	/// 下载缓存区
	uint mDataLength;	/// 下载数据长度

	FileExpressListener* mListener;

	int mState;

	CELL_MUTEX(mMonitor);
	CELL_THREAD_SYNCHRONISER(mWait);

	char* mReceiveCache;

	SingleFileStorage* mFileStorage;	/// 文件存储
};
} // end namespace cell

#endif // CellFileExpressTask_h
